package com.cmnit.analysis.util

import java.security.MessageDigest
import java.util
import java.util.Date

import com.alibaba.fastjson.JSON
import com.cmnit.analysis.bean.SplitProvince

object ServiceUtil {

  /**
   * 生成车牌的MD5
   *
   * @param vehiclePlate 字符串
   * @return MD5前四位
   */
  def getVehiclePlateMD5(vehiclePlate: String): String = {
    try {
      // 此 MessageDigest 类为应用程序提供信息摘要算法的功能
      val md5 = MessageDigest.getInstance("MD5")
      // 转换为MD5码
      val digest = md5.digest(vehiclePlate.getBytes("gbk"))
      val iLen = digest.length
      // 每个byte(8位)用两个(16进制)字符才能表示，所以字符串的长度是数组长度的两倍
      val sb = new StringBuilder(iLen * 2)
      for (byte <- digest) {
        var tmp = byte.toInt
        // 把负数转换为正数
        while (tmp < 0) {
          tmp = tmp + 256
        }
        // 小于0F的数需要在前面补0
        if (tmp < 16) sb.append("0")
        sb.append(Integer.toString(tmp, 16))
      }
      sb.toString.toUpperCase
    } catch {
      case e: Exception =>
        e.printStackTrace()
        ""
    }
  }

  /**
   * 出口车道统计费用
   *
   * @param multiProvince 是否多省交易（0-单省，1-多省）
   * @param fee           总交易金额
   * @param splitProvince 收费数据明细
   * @return
   */
  def getFee(multiProvince: String, fee: String, splitProvince: String): String = {
    if ("0".equals(multiProvince) || "0".equals(fee)) {
      fee
    } else {
      var fee = "0"
      var splitProvinceAfter = ""
      if (splitProvince != null && splitProvince.startsWith("\"")) {
        splitProvinceAfter = splitProvince.substring(1, splitProvince.length - 1)
      } else {
        splitProvinceAfter = splitProvince
      }
      val provinces: util.List[SplitProvince] = JSON.parseArray(splitProvinceAfter, classOf[SplitProvince])
      if (provinces != null) {
        provinces.remove(null)
        import scala.collection.JavaConversions._
        if (provinces.size() > 0) {
          for (province <- provinces) {
            if (province != null && province.provinceId != null) {
              if (province.provinceId.startsWith("36") || province.provinceId.startsWith("24")) {
                fee = province.tollFee
              }
            } else {
              "0"
            }
          }
        }
      }
      fee
    }
  }

  /**
   * 门架统计费用
   *
   * @param mediaType          通行介质类型
   * @param obuprovfeesumafter 本次交易后标签累计金额（省内）
   * @param feeSumLocalAfter   CPC卡省内计费信息
   * @return
   */
  def getFeeFromType(mediaType: String, obuprovfeesumafter: String, feeSumLocalAfter: String): String = {
    if ("1".equals(mediaType)) {
      obuprovfeesumafter
    } else {
      feeSumLocalAfter
    }
  }

  /**
   * 获取指定时间对象前一天的年月日小时
   *
   * @param date 日期对象
   * @return
   */
  def getLastDayInfo(date: Date): (String, String, String, String) = {
    val year = DateUtil.getLastDayOfYear(date)
    val month = DateUtil.getLastDayOfMonth(date)
    val day = DateUtil.getLastDay(date)
    val hour = DateUtil.getLastHour(date)
    (year, month, day, hour)
  }

  /**
   * 获取指定时间对象前N小时的年月日小时
   *
   * @param date 日期对象
   * @return
   */
  def getLastHourInfo(date: Date, time: Integer): (String, String, String, String) = {
    val year = DateUtil.getLastHourOfYear(date, time)
    val month = DateUtil.getLastHourOfMonth(date, time)
    val day = DateUtil.getLastHourOfDay(date, time)
    val hour = DateUtil.getLastHour(date, time)
    (year, month, day, hour)
  }

  /**
   * 获取账期时间（前一个小时）
   *
   * @return
   */
  def getAcctDateOfOneHour(date: Date): String = {
    val (acctYear, acctMonth, acctDay, acctHour) = getLastHourInfo(date, 1)
    acctYear + acctMonth + acctDay + acctHour
  }

  /**
   * 获取账期时间（前两个小时）
   *
   * @return
   */
  def getAcctDateOfTwoHour(date: Date): String = {
    val (acctYear, acctMonth, acctDay, acctHour) = getLastHourInfo(date, 2)
    acctYear + acctMonth + acctDay + acctHour
  }

  /**
   * 获取账期时间的SQL拼接
   *
   * @return
   */
  def getAcctInfo(acctTime: String): String = {
    // 拼接年份
    var sql = "year='" + acctTime.substring(0, 4) + "' "
    // 拼接月份
    if (acctTime.length >= 6) {
      sql += "and month='" + acctTime.substring(4, 6) + "' "
    }
    // 拼接日期
    if (acctTime.length >= 8) {
      sql += "and day='" + acctTime.substring(6, 8) + "' "
    }
    // 拼接小时
    if (acctTime.length >= 10) {
      sql += "and hour='" + acctTime.substring(8, 10) + "' "
    }
    sql
  }
}
